package org.thss.petri.gef.actions;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.thss.petri.gef.commands.FireTransitionCommand;
import org.thss.petri.gef.editparts.TransitionEditPart;
import org.thss.petri.model.Transition;

public class FireTransitionAction extends SelectionAction {

	public FireTransitionAction(IWorkbenchPart window) {
		super(window);
		
		setLazyEnablementCalculation(false);
	}
	
	
	@Override
	protected void init() {
		super.init();
		setText("Fire Transition");
		setId("fire_transition_action");
		setToolTipText("Fire the selected transition");
		ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
		setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));
		setDisabledImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD_DISABLED));
		setEnabled(false);
	}


	@Override
	protected boolean calculateEnabled() {
		Command cmd = createFireTransitionCommand();
		
		if (cmd == null){
			return false;
		}
		return cmd.canExecute();
	}
	
	public Command createFireTransitionCommand(){
		Request request = new Request("fire_transition_action");
		
		List objects = getSelectedObjects();
		if(objects.isEmpty()){
			return null;
		}
		if( ! (objects.get(0) instanceof TransitionEditPart)){
			return null;
		}
		
		EditPart editpart = (EditPart)objects.get(0);
		Command cmd = editpart.getCommand(request);
		return cmd;
	}
		
	
	public void run(){
		execute(createFireTransitionCommand());
	}
	
	
	
}
